local class = reload("class")
local super = reload("super.basic")
local spdlog = require("spdlog")
local t2short = reload("t2short")
local isdebug = cgzMode == "debug"

---@class spdlogLevel
local level = {
    trace = 0,
    debug = 1,
    info = 2,
    warn = 3,
    err = 4,
    critical = 5,
}

---@class  spdlogL : super.basic
local this = class(super)

function this:ctor(name)
    super.ctor(self)
    self._name = name
    local cobj = spdlog.new(name, self)
    self._cobj = cobj
end

function this:setting(prefix)
    return spdlog.set(self._cobj, prefix, isdebug)
end

function this:launch()
end

function this:syslog(fid)
    ---系统日志-关联给c++的日志
    spdlog.run(self._name, self._cobj, fid, level.err)
end

function this:concat(...)
    local info = debug.getinfo(4, "Sln")
    local bstr = string.format("%s:%d %s ", info.source, info.currentline, info.name or "?")
    local len, list = t2short(bstr)
    local alen = select("#", ...)

    if alen > 0 then
        local arg = select(1, ...)
        len, list = t2short(arg, nil, nil, len)
    end
    for i = 2, alen do
        local arg = select(i, ...)
        len, list = t2short(arg, " ", nil, len)
    end
    return len, list
end

---跟踪日志
function this:trace(fid, ...)
    local len, list = self:concat(...)
    spdlog.log(self._name, fid, level.trace, len, list)
end

---调试日志
function this:debug(fid, ...)
    local len, list = self:concat(...)
    spdlog.log(self._name, fid, level.debug, len, list)
end

---信息日志
function this:info(fid, ...)
    local len, list = self:concat(...)
    spdlog.log(self._name, fid, level.info, len, list)
end

---警告日志
function this:warn(fid, ...)
    local len, list = self:concat(...)
    spdlog.log(self._name, fid, level.warn, len, list)
end

---错误日志
function this:err(fid, ...)
    local len, list = self:concat(...)
    spdlog.log(self._name, fid, level.err, len, list)
end

---严重错误日志
function this:critical(fid, ...)
    local len, list = self:concat(...)
    spdlog.log(self._name, fid, level.critical, len, list)
end

return this
